package cn.juque.lucenecandy.core.base;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ClassUtil;
import cn.juque.common.base.PageInfo;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>QueryWrapperBuilder</li>
 * </ul>
 * @date 2023-12-26
 **/
@Slf4j
@EqualsAndHashCode
@ToString
public class QueryWrapperBuilder<T extends BaseEntity> extends AbstractBuilder<T, QueryWrapperBuilder<T>> {

    private PageInfo pageInfo;


    public QueryWrapperBuilder(Class<T> clazz) {
        super(clazz);
        super.thisType = this;
    }

    public QueryWrapperBuilder<T> pageInfo(PageInfo page) {
        this.pageInfo = page;
        return this.thisType;
    }

    @Override
    public QueryWrapper build() {
        Sort sort = Sort.INDEXORDER;
        if(CollUtil.isNotEmpty(super.sortFieldList)) {
            sort = new Sort(ArrayUtil.toArray(super.sortFieldList, SortField.class));
        }
        return new QueryWrapper(
                ClassUtil.getClassName(super.indexClass, false),
                super.indexInfo, super.queryMap, sort, this.pageInfo, super.fieldToLoad);
    }
}
